package com.example.elastic.controller;

import com.example.elastic.entity.Song;
import com.example.elastic.mapper.SongMapper;
import com.example.elastic.repository.SongRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/song", produces = "application/json;charset=utf-8")
public class SongController {

    @Autowired
    private SongMapper songMapper;

    @Autowired
    private SongRepository repository;

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @GetMapping("/initData")
    public void initData() {
        List<Song> list = songMapper.selectAll();

        for (Song song : list) {
            repository.save(song);
        }
    }

    @GetMapping("/selectAll")
    public List<Song> selectAll() {
        return songMapper.selectAll();
    }

    @GetMapping("/deleteAll")
    public void deleteAll() {
        repository.deleteAll();
    }

}